/*
 * Copyright 2025 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */

import QtQml 2.15
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import org.ukui.quick.items 1.0 as UkuiItems
import org.ukui.quick.platform 1.0

Item {
    id: spinbox

    property alias labelText: label.text
    property int itemIndex: -1
    property bool enableBackground: true
    property int minValue: 0
    property int maxValue: 100
    property int value: 0
    property int step: 1
    property int layoutMargins: 0

    signal spinValueChanged(int value, int index)

    UkuiItems.StyleBackground {
        anchors.fill: parent
        useStyleTransparency: false
        visible: enableBackground
    }

    RowLayout {
        spacing: 0
        anchors.fill: parent
        anchors.leftMargin: layoutMargins
        anchors.rightMargin: layoutMargins

        UkuiItems.StyleText {
            id: label

            elide: Qt.ElideRight
            Layout.alignment: Qt.AlignLeft
            Layout.fillWidth: true
        }

        UkuiItems.Background {
            id: control

            Layout.alignment: Qt.AlignRight
            color: Theme.button()
            Theme.onPaletteChanged: {
                color = Theme.button();
            }
            radius: 6
            Layout.maximumWidth: 180
            Layout.minimumWidth: 60
            Layout.preferredWidth: 180
            Layout.fillWidth: false
            implicitWidth: 180
            implicitHeight: 36
            Layout.columnSpan: 0

            RowLayout {
                anchors.fill: parent
                spacing: 5

                UkuiItems.StyleText {
                    text: value
                    Layout.leftMargin: 8
                    horizontalAlignment: Text.AlignLeft
                    verticalAlignment: Text.AlignVCenter
                    Layout.fillHeight: true
                }

                ColumnLayout {
                    spacing: 0
                    Layout.alignment: Qt.AlignRight
                    width: 36
                    height: 36

                    Rectangle {
                        id: upBtn

                        width: parent.width
                        height: parent.height / 2
                        color: upMouse.pressed ? Theme.midLight() : Theme.button()
                        radius: 6

                        MouseArea {
                            id: upMouse

                            anchors.fill: parent
                            onClicked: {
                                var newValue = Math.min(spinbox.maxValue, spinbox.value + spinbox.step);
                                if (value !== newValue) {
                                    value = newValue;
                                    spinValueChanged(value, spinbox.itemIndex);
                                }
                            }
                        }

                        UkuiItems.Icon {
                            anchors.centerIn: parent
                            width: 16
                            height: 16
                            source: "ukui-up-symbolic"
                            mode: UkuiItems.Icon.AutoHighlight
                        }

                    }

                    Rectangle {
                        id: downBtn

                        width: parent.width
                        height: parent.height / 2
                        color: downMouse.pressed ? Theme.midLight() : Theme.button()
                        radius: 6

                        MouseArea {
                            id: downMouse

                            anchors.fill: parent
                            onClicked: {
                                var newValue = Math.max(spinbox.minValue, spinbox.value - spinbox.step);
                                if (value !== newValue) {
                                    value = newValue;
                                    spinValueChanged(value, spinbox.itemIndex);
                                }
                            }
                        }

                        UkuiItems.Icon {
                            anchors.centerIn: parent
                            width: 16
                            height: 16
                            source: "ukui-down-symbolic"
                            mode: UkuiItems.Icon.AutoHighlight
                        }

                    }

                }

            }

        }

    }

}
